package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.DeptDto;
import cn.lll.vuehouse.entity.Dept;
import cn.lll.vuehouse.entity.User;
import cn.lll.vuehouse.mapper.DeptMapper;
import cn.lll.vuehouse.mapper.UserMapper;
import cn.lll.vuehouse.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author asus-pc
* @description 针对表【sys_dept】的数据库操作Service实现
* @createDate 2023-09-01 11:34:22
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
    implements DeptService{

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    UserMapper userMapper;
    @Override
    public ResponseResult getDeptsList() {
        List<Dept> depts = deptMapper.selectList(null);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, depts);
        return responseResult;
    }

    @Override
    public ResponseResult getCurrentPageDeptInfo(DeptDto deptDto) {
        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq(StringUtils.isNotEmpty(deptDto.getName()), "name", deptDto.getName());
        List<Dept> depts = deptMapper.selectList(deptQueryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", depts);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, map);
        return responseResult;
    }

    @Override
    public ResponseResult deleteByIdDept(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("deptid", id);
        List<User> users = userMapper.selectList(userQueryWrapper);
        if(users.size() > 0){
             responseResult.fail(ResponseStatus.DEPT_HAS_PERSON);
             return responseResult;
        }
        deptMapper.deleteById(id);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult addAndUpdateDept(Dept dept) {
        ResponseResult responseResult = new ResponseResult();
        if(ObjectUtil.isNotEmpty(dept.getId())){
            QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
            deptQueryWrapper.eq("name", dept.getName())
                    .ne("id", dept.getId());
            Dept de = deptMapper.selectOne(deptQueryWrapper);
            if(ObjectUtil.isNotEmpty(de)){
                responseResult.fail(ResponseStatus.DEPT_EXITS);
                return responseResult;
            }
            deptMapper.updateById(dept);
            responseResult.success(ResponseStatus.SUCCESS);
            return responseResult;
        }

        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("name", dept.getName());
        Dept de = deptMapper.selectOne(deptQueryWrapper);
        if(ObjectUtil.isNotEmpty(de)){
            responseResult.fail(ResponseStatus.DEPT_EXITS);
            return responseResult;
        }
        // 部门默认创建时间
        dept.setCreatetime(new Date());
        deptMapper.insert(dept);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }


}




